package com.ruoyi.management.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.management.mapper.CommissionManagementMapper;
import com.ruoyi.management.domain.CommissionManagement;
import com.ruoyi.management.service.ICommissionManagementService;
import com.basic.service.impl.BaseServiceImpl;

/**
 * 佣金管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-17
 */
@Service
public class CommissionManagementServiceImpl extends BaseServiceImpl<Long, CommissionManagement> implements ICommissionManagementService 
{
    public CommissionManagementServiceImpl(CommissionManagementMapper baseMapper) {
		super(baseMapper);
		// TODO Auto-generated constructor stub
	}
    /**
     * 查询佣金管理
     * 
     * @param id 佣金管理主键
     * @return 佣金管理
     */
    @Override
    public CommissionManagement selectById(Integer id)
    {
        return baseMapper.selectById(id);
    }

    /**
     * 查询佣金管理列表
     * 
     * @param commissionManagement 佣金管理
     * @return 佣金管理
     */
    @Override
    public List<CommissionManagement> selectList(CommissionManagement commissionManagement)
    {
        return baseMapper.selectList(commissionManagement);
    }

    /**
     * 新增佣金管理
     * 
     * @param commissionManagement 佣金管理
     * @return 结果
     */
    @Override
    public int insert(CommissionManagement commissionManagement)
    {
        return baseMapper.insert(commissionManagement);
    }

    /**
     * 修改佣金管理
     * 
     * @param commissionManagement 佣金管理
     * @return 结果
     */
    @Override
    public int update(CommissionManagement commissionManagement)
    {
        return baseMapper.update(commissionManagement);
    }

    /**
     * 批量删除佣金管理
     * 
     * @param ids 需要删除的佣金管理主键
     * @return 结果
     */
    @Override
    public int deleteByIds(Integer[] ids)
    {
        return baseMapper.deleteByIds(ids);
    }

    /**
     * 删除佣金管理信息
     * 
     * @param id 佣金管理主键
     * @return 结果
     */
    @Override
    public int deleteById(Integer id)
    {
        return baseMapper.deleteById(id);
    }
    
	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public CommissionManagement getCommissonManagent(long userId) {
		
		CommissionManagementMapper mapper = this.getBaseMapper();
		
		return mapper.getCommissonManagentForUpdate(userId);
	}
	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public int parseCommissionManagement(long userId, String nickName) {
		
		CommissionManagement item = this.getCommissonManagent(userId);
		
		long timeMillis = System.currentTimeMillis();
		
		if (item == null) {
			// 添加
			
			// 生成 CommissionManagement 对象
			CommissionManagement commissonManagent = new CommissionManagement();
            commissonManagent.setCreateTime(timeMillis);
            commissonManagent.setUpdateTime(timeMillis);
            
            commissonManagent.setRecommenNum(1);
            
            BigDecimal zero = new BigDecimal("0");
            
            commissonManagent.setFreezeAmount(zero);
            
            commissonManagent.setTotalAmount(zero);
            
            commissonManagent.setUserId((int) userId);
            
            commissonManagent.setUserName(nickName);
            
            commissonManagent.setWithdrawableAmount(zero);
            
            this.insert(commissonManagent);
            
            return 1;
		}
		
		CommissionManagement commissionManagement = new CommissionManagement();
		
		commissionManagement.setId(item.getId());
		
		commissionManagement.setUserName(nickName);
		
		commissionManagement.setRecommenNum(item.getRecommenNum() + 1);
		
		return this.update(commissionManagement);
	}
	@Override
	public int updateMoney(CommissionManagement commissionManagement) {
		
		CommissionManagementMapper mapper = this.getBaseMapper();

		
		
		return mapper.updateMoney(commissionManagement);
	}
}
